<template>
  <div class="image-uploader">
    <input :id="componentUid" type="file" multiple accept="image/*" class="file-input" @change="handleFileChange" />
    <label :for="componentUid" class="upload-button">
      <IconifyIconOffline icon="ep:plus" width="28" height="28" style="color: var(--el-text-color-secondary)" />
    </label>
    <div class="image-list">
      <div
        v-for="(image, index) in data"
        :key="index"
        class="image-item"
        draggable
        @dragstart="dragStart(index)"
        @dragover.prevent="dragOver(index)"
        @drop="drop()"
      >
        <img :src="image" class="preview-image" />
        <div class="image-overlay">
          <IconifyIconOffline icon="ep:close" class="remove-btn" @click="removeImage(index)" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { http } from "@/utils/http";
import { ref, triggerRef, watch, type PropType, getCurrentInstance } from "vue";
const data = defineModel({ type: Array as PropType<string[]>, default: () => [] });
const prop = defineProps({
  size: {
    type: Number,
    default: 148
  }
});
const instance = getCurrentInstance();
const componentUid = "file-upload" + instance?.uid;
const draggedIndex = ref<number | null>(null);

const handleFileChange = (e: Event) => {
  const target = e.target as HTMLInputElement;
  const files = Array.from(target.files || []);

  files.forEach(file => {
    if (!file.type.match("image.*")) return;

    http
      .post<MResponse<string>>("/user/uploadFile", {
        headers: { "content-type": "multipart/form-data" },
        data: { file: file }
      })
      .then(res => {
        if (res.code == RESPONSE_CODE.SUCCESS) {
          data.value.push(res.data);
          triggerRef(data);
        }
      });
  });
  target.value = "";
};

const removeImage = (index: number) => {
  data.value.splice(index, 1);
};

const dragStart = (index: number) => {
  draggedIndex.value = index;
};

const dragOver = (index: number) => {
  if (draggedIndex.value === null || draggedIndex.value === index) return;

  const draggedItem = data.value[draggedIndex.value];
  const newImages = [...data.value];
  newImages.splice(draggedIndex.value, 1);
  newImages.splice(index, 0, draggedItem);
  data.value = newImages;
  draggedIndex.value = index;
};

const drop = () => {
  draggedIndex.value = null;
};
</script>

<style scoped>
.image-uploader {
  max-width: 800px;
  margin: 0 auto;
  padding: 16px;
  background: transparent;
  border-radius: 0;
  box-shadow: none;
  border: 1px solid #d1d5db;
}

.file-input {
  display: none;
}

.upload-button {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: v-bind(size + "px");
  height: v-bind(size + "px");
  padding: 0;
  border: 1px dashed var(--el-border-color-darker);
  background-color: #f9fafb;
  color: #6b7280;
  cursor: pointer;
  transition: all 0.3s;
  box-sizing: border-box;
}

.upload-button:hover {
  border-color: var(--el-color-primary);
}

.file-input-label {
  display: inline-block;
  margin-bottom: 24px;
  padding: 12px 24px;
  background: var(--el-color-primary);
  color: white;

  cursor: pointer;
  font-weight: 500;
  transition: all 0.3s;
}

.file-input-label:hover {
  background: var(--el-color-primary);
}

.image-list {
  display: flex;
  flex-direction: column;
  width: v-bind(size + "px");
}

.image-item {
  position: relative;
  width: v-bind(size + "px");
  height: v-bind(size + "px");
  z-index: 2;
}

.preview-image {
  width: v-bind(size + "px");
  height: v-bind(size + "px");
  object-fit: cover;
}

.image-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0;
  transition: all 0.3s;
  display: flex;
  justify-content: flex-end;
  align-items: flex-start;
  z-index: 1;
  pointer-events: none;
}

.image-item:hover .image-overlay {
  opacity: 1;
}

.remove-btn {
  background: transparent;
  color: white;
  cursor: pointer;
  font-size: 14px;
  text-align: center;
  opacity: 1;
  transition: all 0.2s;
  pointer-events: auto;
  z-index: 3;
  margin: 8px;
}

.remove-btn:hover {
  transform: scale(1.1);
}

.image-item:hover .remove-btn {
  opacity: 1;
}
</style>
